﻿@using AC.Service.DTO.LiCai
@model wychuan2.com.Areas.admin.Models.LiCai.BillModel

@{
    List<ItemDTO> lstBooks = Model.Items.Where(m => m.Type == ItemType.BillBook).ToList();
    List<ItemDTO> lstProjects = Model.Items.Where(m => m.Type == ItemType.Project).ToList();
    List<ItemDTO> lstMembers = Model.Items.Where(m => m.Type == ItemType.Member).ToList();
    List<ItemDTO> lstBusiness = Model.Items.Where(m => m.Type == ItemType.Business).ToList();
    List<ItemDTO> lstPaymentPersons = Model.Items.Where(m => m.Type == ItemType.PaymentPerson).ToList();
    
    var lstExpends = Model.Categories.Where(c => c.InOutType == CategoryType.Expend && c.ParentId == 0).OrderByDescending(c => c.IsCommonUse);
    var lstExpendsChilds = Model.Categories.Where(c => c.ParentId == lstExpends.FirstOrDefault().Id).OrderByDescending(c => c.IsCommonUse);

    var lstIncomes = Model.Categories.Where(c => c.InOutType == CategoryType.Income && c.ParentId == 0).OrderByDescending(c => c.IsCommonUse);
    var lstIncomesChilds = Model.Categories.Where(c => c.ParentId == lstIncomes.FirstOrDefault().Id).OrderByDescending(c => c.IsCommonUse);
}

@if (Model.Details != null && Model.Details.Count() > 0)
{
    
    <div class="alert alert-info">
        @if (Model.DetailType != BillDetailType.Creditor.GetHashCode())
        {
            var totalExpendPrice = Model.Details.Where(b => b.DetailType == BillDetailType.Expend.GetHashCode() && b.BaoXiao == 0).Sum(b => b.Price);
            var totalIncomePrice = Model.Details.Where(b => b.DetailType == BillDetailType.Income.GetHashCode()).Sum(b => b.Price);
            if (Model.DetailType != BillDetailType.Income.GetHashCode() && Model.DetailType != BillDetailType.Transfer.GetHashCode())
            {
        <span class="label label-primary">共支出：¥@totalExpendPrice.ToString("0.00")</span>
            }
            if (Model.DetailType != BillDetailType.Expend.GetHashCode() && Model.DetailType != BillDetailType.Transfer.GetHashCode())
            {
        <span class="label label-danger">共收入：¥@totalIncomePrice.ToString("0.00")</span>
            }
        }
        else
        {
            var sumOfShouKuan = Model.Details.Where(b => b.CreditorType == CreditType.ShouKuan.GetHashCode()).Sum(b => b.Price);
            var sumOfJieChu = Model.Details.Where(b => b.CreditorType == CreditType.JieChu.GetHashCode()).Sum(b => b.Price);
            var sumOfHuanKuan = Model.Details.Where(b => b.CreditorType == CreditType.HuanKuan.GetHashCode()).Sum(b => b.Price);
            var sumOfJieRu = Model.Details.Where(b => b.CreditorType == CreditType.JieRu.GetHashCode()).Sum(b => b.Price);
            <span class="label label-primary">他人借你：¥@((sumOfJieChu-sumOfShouKuan).ToString("0.00"))</span>
            <span class="label label-danger">你借他人：¥@((sumOfJieRu-sumOfHuanKuan).ToString("0.00"))</span>
        }
        <a class="btn btn-white J_s">收起</a>
        <a class="btn btn-white J_z" disabled="disabled">展开</a>
        <a class="btn btn-white" href="/admin/licai/fastbill">去记账</a>
    </div>

    IEnumerable<IGrouping<DateTime, BillDTO>> lstDetails = Model.Details.GroupBy(b => b.ConsumeTime.Date);
    foreach (var item in lstDetails)
    {
        DateTime currentDay = item.Key;
        List<BillDTO> lstBills = item.ToList();

        decimal sumOfExpendsPrice = lstBills.Where(b => b.DetailType == BillDetailType.Expend.GetHashCode() && b.BaoXiao == 0).Sum(b => b.Price);
        decimal sumOfIncomePrice = lstBills.Where(b => b.DetailType == BillDetailType.Income.GetHashCode()).Sum(b => b.Price);

        <div class="ibox float-e-margins" style="margin-bottom: 5px;">
            <div class="ibox-title ibox-title-bill" style="padding-top: 0px; padding-bottom: 0px; min-height: 20px; height: 20px;">
                <h5>@currentDay.ToString("yyyy-MM-dd")</h5>
                <div class="ibox-tools">
                    <span class="label label-primary">支：¥@sumOfExpendsPrice.ToString("0.00")</span>
                    <span class="label label-danger">收：¥@sumOfIncomePrice.ToString("0.00")</span>

                    <a class="collapse-link ui-sortable J_BillItem">
                        <i class="fa fa-chevron-up"></i>
                    </a>
                </div>
            </div>
            <div class="ibox-content" style="display: block; padding-top: 5px; padding-bottom: 5px;">
                <div class="bill-item-list">
                    @foreach (var bill in lstBills)
                    {
                        string priceColor = "";
                        string textColor = "text-navy";
                        string text = bill.SecondCategory;
                        string checkedStr = "";
                        if (bill.DetailType == BillDetailType.Expend.GetHashCode())
                        {
                            priceColor = "text-danger";
                            if (bill.BaoXiao == 1)
                            {
                                textColor = "text-danger";
                                checkedStr = "checked='checked'";
                            }
                        }
                        else if (bill.DetailType == BillDetailType.Income.GetHashCode())
                        {
                            priceColor = "text-navy";
                        }
                        else if (bill.DetailType == BillDetailType.Transfer.GetHashCode())
                        {
                            var accountFrom = Model.Accounts.First(a => a.Id == bill.AccountId);
                            var accountTo = Model.Accounts.First(a => a.Id == bill.ToAccountId);
                            text = string.Format("{0} 转 {1}", accountFrom.Name, accountTo.Name);
                            textColor = "text-warning";
                        }
                        else if (bill.DetailType == BillDetailType.Creditor.GetHashCode())
                        {
                            if (bill.CreditorType == CreditType.JieRu.GetHashCode())
                            {
                                text = string.Format("从 {0} 借入", bill.Business);
                            }
                            else if (bill.CreditorType == CreditType.JieChu.GetHashCode())
                            {
                                text = string.Format("借出给 {0}", bill.Business);
                            }
                            else if (bill.CreditorType == CreditType.ShouKuan.GetHashCode())
                            {
                                text = string.Format("从 {0} 收债", bill.Business);
                            }
                            else
                            {
                                text = string.Format("还款给 {0}", bill.Business);
                            }
                            textColor = "text-warning";
                        }

                        
                        <div class="bill-item" id="@("item"+bill.ID)">
                            <div class="row">
                                <div class="col-md-12">
                                    <small class="pull-right bill-item-price @priceColor">@bill.Price</small>
                                    <a data-toggle="collapse" class="@textColor" href="#@bill.ID">@text</a>
                                    <small><i class="fa fa-clock-o"></i>@bill.ConsumeTime.ToString("HH:mm")  <strong>@bill.Remark</strong> </small>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12" style="padding-left: 0px; padding-right: 0px;">
                                    <div id="@bill.ID" class="panel-collapse faq-answer collapse" aria-expanded="false">
                                        @if (bill.DetailType == BillDetailType.Income.GetHashCode() || bill.DetailType == BillDetailType.Expend.GetHashCode())
                                        {
                                            var businessLabel = bill.DetailType == BillDetailType.Expend.GetHashCode() ? "商家" : "付款方";
                                            var lstFirst = lstExpends;
                                            var businessLst = lstBusiness;
                                            if (bill.DetailType == BillDetailType.Income.GetHashCode())
                                            {
                                                lstFirst = lstIncomes;
                                                businessLst = lstPaymentPersons;
                                            }
                                            <form class="form-horizontal" name="billEditForm">
                                                <input type="hidden" name="Id" value="@bill.ID" />
                                                <div class="row">
                                                    <div class="col-md-6">
                                                        <div class="form-group m-b-xxs">
                                                            @*<label class="col-md-1 sr-only control-label">金额</label>*@
                                                            <div class="col-md-offset-1 col-md-11">
                                                                <div class="input-group">
                                                                    <span class="input-group-addon">￥</span>
                                                                    <input name="txtPrice" type="text" value="@bill.Price" class="form-control" />
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="form-group m-b-xxs">
                                                            @*<label class="col-sm-1 control-label">时间</label>*@
                                                            <div class="col-sm-12">
                                                                <input name="txtConsumeTime" type="text" class="form-control datetimepicker" value="@bill.ConsumeTime.ToString("yyyy-MM-dd HH:mm")" />
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-md-6">
                                                        <div class="form-group m-b-xxs">
                                                            @*<label class="col-sm-1 control-label">备注</label>*@
                                                            <div class="col-md-offset-1 col-md-11">
                                                                <input type="text" name="txtRemark" class="form-control" placeholder="备注信息" value="@bill.Remark" />
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="form-group m-b-xxs">
                                                            @*<label class="sr-only control-label">类别</label>*@

                                                            <div class="col-md-12">
                                                                <div class="input-group">
                                                                    <select class="input-sm form-control" name="selFirstCategory">
                                                                        @foreach (var first in lstFirst)
                                                                        {
                                                                            <option @(first.Id == bill.FirstCategoryId ? "selected" : "") value="@first.Id">@first.Name</option>
                                                                        }
                                                                    </select>
                                                                    <span class="input-group-addon">-</span>
                                                                    <select class="input-sm form-control" name="selCategory">
                                                                        @{
                                                                            var lstSeconds = Model.Categories.Where(c => c.ParentId == bill.FirstCategoryId).OrderByDescending(c => c.IsCommonUse);
                                                                        }
                                                                        @foreach (var second in lstSeconds)
                                                                        {
                                                                            <option @(second.Id == bill.SecondCategoryId ? "selected" : "") value="@second.Id">@second.Name</option>
                                                                        }
                                                                    </select>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-md-12">
                                                        <div class="input-group col-md-offset-1 m-b-xxs">
                                                            <select class="input-sm" name="selAccount">
                                                                @foreach (var account in Model.Accounts.Where(a => a.IsEnable))
                                                                {
                                                                    <option @(account.Id == bill.AccountId ? "selected" : "") value="@account.Id">@(account.Name + "(" + account.Currency + ")")</option>
                                                                }
                                                            </select>
                                                            <select class="input-sm" name="selBusiness">
                                                                <option value="0">@businessLabel</option>
                                                                @foreach (var business in businessLst)
                                                                {
                                                                    <option @(business.Id == bill.BusinessId ? "selected" : "") value="@business.Id">@business.Name</option>
                                                                }
                                                            </select>
                                                            <select class="input-sm" name="selMember">
                                                                @foreach (var member in lstMembers)
                                                                {
                                                                    <option @(member.Id == bill.MemberId ? "selected" : "") value="@member.Id">@member.Name</option>
                                                                }
                                                            </select>
                                                            <select class="input-sm" name="selProject">
                                                                @foreach (var project in lstProjects)
                                                                {
                                                                    <option @(project.Id == bill.ProjectId ? "selected" : "") value="@project.Id">@project.Name</option>
                                                                }
                                                            </select>
                                                            @if (bill.DetailType == BillDetailType.Expend.GetHashCode())
                                                            {
                                                                <label> <input type="checkbox" name="chkBaoXiao" class="i-checks" @checkedStr /> 可报销 </label>
                                                            }
                                                        </div>
                                                    </div>
                                                </div>
                                                <hr class="m-sm" />
                                                <div class="row">
                                                    <div class="col-md-12">
                                                        <div class="form-group m-b-sm">
                                                            <div class="col-md-offset-1 col-md-11">
                                                                <button name="btnSave" class="btn btn-primary" type="button"><strong>保存</strong></button>
                                                                <button name="btnDelete" class="btn btn-primary" type="button"><strong>删除</strong></button>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </form>
                                        }
                                        else if (bill.DetailType == BillDetailType.Transfer.GetHashCode())
                                        {
                                            <form class="form-horizontal" name="billEditForm">
                                                <input type="hidden" name="Id" value="@bill.ID" />
                                                <div class="row">
                                                    <div class="col-md-6">
                                                        <div class="form-group m-b-xxs">
                                                            @*<label class="col-md-1 sr-only control-label">金额</label>*@
                                                            <div class="col-md-offset-1 col-md-11">
                                                                <div class="input-group">
                                                                    <span class="input-group-addon">￥</span>
                                                                    <input name="txtPrice" type="text" value="@bill.Price" class="form-control" />
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="form-group m-b-xxs">
                                                            @*<label class="col-sm-1 control-label">时间</label>*@
                                                            <div class="col-sm-12">
                                                                <input name="txtConsumeTime" type="text" class="form-control datetimepicker" value="@bill.ConsumeTime.ToString("yyyy-MM-dd HH:mm")" />
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-md-6">
                                                        <div class="form-group m-b-xxs">
                                                            @*<label class="col-sm-1 control-label">备注</label>*@
                                                            <div class="col-md-offset-1 col-md-11">
                                                                <input type="text" name="txtRemark" class="form-control" placeholder="备注信息" value="@bill.Remark" />
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="form-group m-b-xxs">
                                                            @*<label class="sr-only control-label">账户</label>*@

                                                            <div class="col-md-12">
                                                                <div class="input-group">
                                                                    <select class="input-sm form-control" name="selAccount">
                                                                        @foreach (var account in Model.Accounts.Where(a => a.IsEnable))
                                                                        {
                                                                            <option @(account.Id == bill.AccountId ? "selected" : "") value="@account.Id">@(account.Name + "(" + account.Currency + ")")</option>
                                                                        }
                                                                    </select>
                                                                    <span class="input-group-addon">-</span>
                                                                    <select class="input-sm form-control" name="selAccountTo">
                                                                        @foreach (var account in Model.Accounts.Where(a => a.IsEnable))
                                                                        {
                                                                            <option @(account.Id == bill.ToAccountId ? "selected" : "") value="@account.Id">@(account.Name + "(" + account.Currency + ")")</option>
                                                                        }
                                                                    </select>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <hr class="m-sm" />
                                                <div class="row">
                                                    <div class="col-md-12">
                                                        <div class="form-group m-b-sm">
                                                            <div class="col-md-offset-1 col-md-11">
                                                                <button name="btnSave" class="btn btn-primary" type="button"><strong>保存</strong></button>
                                                                <button name="btnDelete" class="btn btn-primary" type="button"><strong>删除</strong></button>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </form>
                                        }
                                        else if (bill.DetailType == BillDetailType.Creditor.GetHashCode())
                                        {
                                            <form class="form-horizontal" name="billEditForm">
                                                <input type="hidden" name="Id" value="@bill.ID" />
                                                <div class="row">
                                                    <div class="col-md-6">
                                                        <div class="form-group m-b-xxs">
                                                            @*<label class="col-md-1 sr-only control-label">金额</label>*@
                                                            <div class="col-md-offset-1 col-md-11">
                                                                <div class="input-group">
                                                                    <span class="input-group-addon">￥</span>
                                                                    <input name="txtPrice" type="text" value="@bill.Price" class="form-control" />
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="form-group m-b-xxs">
                                                            @*<label class="col-sm-1 control-label">时间</label>*@
                                                            <div class="col-sm-12">
                                                                <input name="txtConsumeTime" type="text" class="form-control datetimepicker" value="@bill.ConsumeTime.ToString("yyyy-MM-dd HH:mm")" />
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-md-6">
                                                        <div class="form-group m-b-xxs">
                                                            @*<label class="col-sm-1 control-label">备注</label>*@
                                                            <div class="col-md-offset-1 col-md-11">
                                                                <input type="text" name="txtRemark" class="form-control" placeholder="备注信息" value="@bill.Remark" />
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="form-group m-b-xxs">
                                                            @*<label class="sr-only control-label">类别</label>*@
                                                            <div class="col-md-12">
                                                                <select class="form-control" name="selCreditorType">
                                                                    <option @(1 == bill.CreditorType ? "selected" : "") value="1">借入</option>
                                                                    <option @(2 == bill.CreditorType ? "selected" : "") value="2">借出</option>
                                                                    <option @(3 == bill.CreditorType ? "selected" : "") value="3">收款</option>
                                                                    <option @(4 == bill.CreditorType ? "selected" : "") value="4">还款</option>
                                                                </select>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-md-12">
                                                        <div class="input-group col-md-offset-1 m-b-xxs">
                                                            <select class="input-sm" name="selBusiness">
                                                                <option value="0">债权人</option>
                                                                @foreach (var creditor in Model.Items.Where(m => m.Type == ItemType.Creditor))
                                                                {
                                                                    <option @(creditor.Id == bill.BusinessId ? "selected" : "") value="@creditor.Id">@creditor.Name</option>
                                                                }
                                                            </select>
                                                            <select class="input-sm" name="selAccount">
                                                                @foreach (var account in Model.Accounts.Where(a => a.IsEnable))
                                                                {
                                                                    <option @(account.Id == bill.AccountId ? "selected" : "") value="@account.Id">@(account.Name + "(" + account.Currency + ")")</option>
                                                                }
                                                            </select>

                                                            <input name="txtRefundTime" type="text" class="input-sm datetimepicker" value="@bill.RefundTime" placeholder="还款时间" />

                                                            <select class="input-sm" name="selRefundNotice">
                                                                <option @(bill.RefundNotice.Equals("无需提醒") ? "selected" : "")>无需提醒</option>
                                                                <option @(bill.RefundNotice.Equals("提醒一次") ? "selected" : "")>提醒一次</option>
                                                                <option @(bill.RefundNotice.Equals("每月提醒") ? "selected" : "")>每月提醒</option>
                                                            </select>
                                                        </div>
                                                    </div>
                                                </div>
                                                <hr class="m-sm" />
                                                <div class="row">
                                                    <div class="col-md-12">
                                                        <div class="form-group m-b-sm">
                                                            <div class="col-md-offset-1 col-md-11">
                                                                <button name="btnSave" class="btn btn-primary" type="button"><strong>保存</strong></button>
                                                                <button name="btnDelete" class="btn btn-primary" type="button"><strong>删除</strong></button>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </form>
                                        }
                                    </div>
                                </div>
                            </div>
                        </div>
                    }
                </div>
            </div>
        </div>
    }
}
else
{
    <div class="ibox-content">
        暂时没有账单明细。
    </div>
}
